package com.xiaobing.auth.service.login;

import com.xiaobing.common.constant.CacheConstants;
import com.xiaobing.common.entity.system.SysUser;
import com.xiaobing.common.exception.ExceptionCodeMsg;
import com.xiaobing.common.exception.ServiceException;
import com.xiaobing.common.redis.RedisService;
import com.xiaobing.common.utils.SecurityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 登录密码方法
 *
 * @author xulk
 */
@Component
public class SysPasswordService {
    @Resource
    private RedisService redisService;

    /**
     * 登录账户密码错误次数缓存键名
     *
     * @param username 用户名
     * @return 缓存键key
     */
    private String getCacheKey(String username) {
        return CacheConstants.PWD_ERR_CNT_KEY + username;
    }

    public void validate(SysUser user, String password) {
        if (!matches(user, password)) {
            throw new ServiceException(ExceptionCodeMsg.FAILURE);
        }
    }

    public boolean matches(SysUser user, String rawPassword) {
        return SecurityUtils.matchesPassword(rawPassword, user.getPassword());
    }


}
